// MFCLab2View.cpp : implementation of the CMFCLab2View class
//

#include "stdafx.h"
#include "MFCLab2.h"
#include <GL/gl.h> 
#include <GL/glu.h>

#include "MFCLab2Doc.h"
#include "MFCLab2View.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCLab2View

IMPLEMENT_DYNCREATE(CMFCLab2View, CView)

BEGIN_MESSAGE_MAP(CMFCLab2View, CView)
	// Standard printing commands
	ON_COMMAND (ID_SELECT, OnToolBarButtonSelect)
	ON_COMMAND (ID_LINE, OnToolBarButtonLine)
	ON_COMMAND (ID_RECTANGLE, OnToolBarButtonRectangle)
	ON_COMMAND (ID_CIRCLE, OnToolBarButtonCircle)
	ON_COMMAND (ID_POLYGON, OnToolBarButtonPolygon)
	ON_COMMAND (ID_CUBE, OnToolBarButtonCube)
	ON_COMMAND (ID_SPHERE, OnToolBarButtonSphere)
	ON_COMMAND (ID_CYLINDER, OnToolBarButtonCylinder)
	ON_COMMAND (ID_TORUS, OnToolBarButtonTorus)
	ON_UPDATE_COMMAND_UI(ID_TORUS, OnUpdateToolBarButtonTorus)
	ON_UPDATE_COMMAND_UI(ID_SELECT, OnUpdateToolBarButtonSelect)
	ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateToolBarButtonLine)
	ON_UPDATE_COMMAND_UI(ID_RECTANGLE, OnUpdateToolBarButtonRectangle)
	ON_UPDATE_COMMAND_UI(ID_CIRCLE, OnUpdateToolBarButtonCircle)
	ON_UPDATE_COMMAND_UI(ID_POLYGON, OnUpdateToolBarButtonPolygon)
	ON_UPDATE_COMMAND_UI(ID_CUBE, OnUpdateToolBarButtonCube)
	ON_UPDATE_COMMAND_UI(ID_SPHERE, OnUpdateToolBarButtonSphere)
	ON_UPDATE_COMMAND_UI(ID_CYLINDER, OnUpdateToolBarButtonCylinder)

	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)

	ON_COMMAND(ID_VIEW_2D, OnView2D)
	ON_COMMAND(ID_VIEW_3D, OnView3D)
	ON_UPDATE_COMMAND_UI(ID_VIEW_2D, OnUpdateView2D)
	ON_UPDATE_COMMAND_UI(ID_VIEW_3D, OnUpdateView3D)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_PAINT()
	ON_WM_DESTROY()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_ERASEBKGND()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

static CMFCLab2View *openGLView = NULL;
void CALLBACK MyTimerProc( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
{
	openGLView->Invalidate();
}
// CMFCLab2View construction/destruction

CMFCLab2View::CMFCLab2View()
: m_state(SELECT)
{
	// TODO: add construction code here

}

CMFCLab2View::~CMFCLab2View()
{
}

BOOL CMFCLab2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CMFCLab2View drawing

void CMFCLab2View::OnDraw(CDC* /*pDC*/)
{
	CMFCLab2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: add draw code for native data here
}


// CMFCLab2View printing

BOOL CMFCLab2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CMFCLab2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CMFCLab2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CMFCLab2View diagnostics

#ifdef _DEBUG
void CMFCLab2View::AssertValid() const
{
	CView::AssertValid();
}

void CMFCLab2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCLab2Doc* CMFCLab2View::GetDocument() const // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCLab2Doc)));
	return (CMFCLab2Doc*)m_pDocument;
}
#endif //_DEBUG


// CLab2View message handlers

void CMFCLab2View::SetupOpenGL(void)
{
	//Declare Pixel Format 
	static PIXELFORMATDESCRIPTOR pfd = 
	{ 
		sizeof(PIXELFORMATDESCRIPTOR), 
		1, 
		PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, 
		PFD_TYPE_RGBA, 
		32, // bit depth 
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
		16, // z-buffer depth 
		0, 0, 0, 0, 0, 0, 0, 
	}; 
	
	// Get device context only once. 
	HDC hdc = GetDC()->m_hDC; 

	// Set Pixel format. 
	int nPixelFormat = ChoosePixelFormat(hdc, &pfd); 
	SetPixelFormat(hdc, nPixelFormat, &pfd); 


	// Create the OpenGL Rendering Context. 
	m_hRC = wglCreateContext(hdc); 
	wglMakeCurrent(hdc, m_hRC); 

	GLfloat LightPosition[]= { -3.0f, -2.0f, 3.0f, 1.0f };
	//GLfloat LightPosition2[]= { 5.0f, 5.0f, 5.0f, 1.0f };
	GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 
	GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };	
	
	glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);	
	glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);	

	glEnable(GL_LIGHT1);

/*	glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmbient);
	glLightfv(GL_LIGHT0, GL_POSITION,LightPosition2);
	glEnable(GL_LIGHT0);*/	
	glEnable(GL_LIGHTING);	
}

int CMFCLab2View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	SetupOpenGL();
	InitParameters();
	
	//MessageBeep(0xFFFFFFFF);
	//::SetTimer(0, 1 , 3, (TIMERPROC)MyTimerProc);
	SetTimer(1, 3, NULL);
	openGLView = this;
	return 0;
}

void CMFCLab2View::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy); 

	//Setting up viewport 
	CRect rect; 
	GetClientRect(rect);
	m_windowWidth = rect.Width();
	m_windowHeight = rect.Height();
	glViewport(0, 0, m_windowWidth, m_windowHeight);

	if (m_windowWidth == 0|| m_windowHeight == 0) return;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	if (m_windowWidth <= m_windowHeight)
	{		
		m_width = 1.0;
		m_height = (float)m_windowHeight /  m_windowWidth;
	}
	else 
	{
		m_height = 1.0;
		m_width = (float)m_windowWidth /  m_windowHeight;
	}
	//Orthogonal
	//glOrtho(-m_width, m_width, -m_height, m_height, -10.0, 10.0);
	
	//Perspective
	glFrustum(-m_width, m_width,-m_height, m_height, 1.0, 20.0);
	//gluPerspective(30.0, m_aspect, 1.0, 20.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void CMFCLab2View::PaintScreen(GLenum mode)
{
	for(int i = 0; i < m_numObject; i++)
	{
		if (mode == GL_SELECT) 
			glLoadName(i);
		m_listObjects[i]->Draw();
	}
}

void CMFCLab2View::OnPaint()
{
	CPaintDC dc(this); // device context for painting 

	wglMakeCurrent(dc.m_hDC, m_hRC);
	
	if (m_mode == TWO_D)
	{
		glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(0.0, 0.0, camera2d_location_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
		glDisable(GL_LIGHTING);
	}
	else
	{
		glEnable (GL_DEPTH_TEST);
		glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		gluLookAt(camera3d_location_x, camera3d_location_y, camera3d_location_z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
		glEnable(GL_LIGHTING);
	}
	
	
	PaintScreen(GL_RENDER);	

	glFlush();

	SwapBuffers(dc.m_ps.hdc); // Swap the front and the back buffer to show the created graphics 
}

void CMFCLab2View::RemoveOpenGL(void)
{
	//Delete Rendering Context 
	if(wglGetCurrentContext() != NULL) 
		wglMakeCurrent(NULL,NULL); 

	if(m_hRC != NULL) 
	{ 
		wglDeleteContext(m_hRC); 
		m_hRC = NULL; 
	}

	// Delete Geometric Objects
	for (int i = 0; i < m_numObject; i++)
	{
		if (m_listObjects[i] != NULL)
		{
			delete m_listObjects[i];
		}
	}
}

void CMFCLab2View::OnDestroy()
{
	CView::OnDestroy(); 
	//Remove openGL 
	RemoveOpenGL();
}

void CMFCLab2View::InitParameters(void)
{
	m_numObject = 0;
	m_isMouseDown = false;
	m_state = SELECT;
	m_mode = TWO_D;
	camera2d_location_z = 1.5;
	camera3d_location_x = 1.5;
	camera3d_location_y = 1.5;
	camera3d_location_z = 1.5;
}

void CMFCLab2View::OnLButtonDown(UINT nFlags, CPoint point)
{
	lastPoint = point;
	m_isMouseDown = true;
	if (m_state == SELECT)
	{
		//clear prev selection
		for (int i = 0; i < m_numObject; i++)
		{
			m_listObjects[i]->selected = false;
		}
		// process Select mode
		GLuint selectBuf[BUFSIZE];
		GLint hits;
		GLint viewport[4];		

		HDC hDC = GetDC()->m_hDC;
		wglMakeCurrent(hDC, m_hRC);

		glGetIntegerv(GL_VIEWPORT, viewport);
		glSelectBuffer(BUFSIZE, selectBuf);

		glRenderMode(GL_SELECT); // Enter the SELECT render mode
		glInitNames();
		glPushName(-1);

		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
			glLoadIdentity();
			gluPickMatrix((GLdouble) point.x, (GLdouble) (viewport[3] - point.y), 5.0, 5.0, viewport);
			glFrustum(-m_width, m_width,-m_height, m_height, 1.0, 20.0);
			//gluPerspective(30.0, m_aspect, 1.0, 20.0);
			glMatrixMode(GL_MODELVIEW);
			PaintScreen(GL_SELECT);
		glPopMatrix();
		glFlush();

		hits = glRenderMode(GL_RENDER);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glViewport(0, 0, m_windowWidth, m_windowHeight);
		glFrustum(-m_width, m_width,-m_height, m_height, 1.0, 20.0);
		//gluPerspective(30.0, m_aspect, 1.0, 20.0);

		// process Hits
		if (hits)
		{
			int n = 0; 
			double minz = selectBuf[1];
			for(int i = 1; i < hits; i++)
			{
				if (selectBuf[1 + i * 4] < minz)
				{
					n = i; 
					minz = selectBuf[1 + i * 4];
				}
			}
			m_listObjects[selectBuf[3 + n * 4]]->selected = !m_listObjects[selectBuf[3 + n * 4]]->selected;
		}
		//Invalidate();
	}
	else 
	{
		CGeometricObject *object;
		
		CRect rect; 
		GetClientRect(rect); 
		//Change from window coordinate system to real coordinate system
		float x = 2* point.x * m_width / rect.Width() - m_width;
		float y = 2*(rect.Height() - point.y)*m_height / rect.Height() - m_height;
		if (m_state == LINE)
		{
			//Create a line object
			CLine* line = new CLine();
			line->getBegin()->setCoords(x, y, 0.0f);
			line->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = line;
			m_numObject++;
		}
		else if (m_state == RECTANGLE)
		{
			//Create a rectangle object
			CRectangle *rect = new CRectangle();
			rect->getBegin()->setCoords(x, y, 0.0f);
			rect->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = rect;
			m_numObject++;
		}
		else if (m_state == CIRCLE)
		{
			//Create a circle object
			CCircle *circle = new CCircle();
			circle->getBegin()->setCoords(x, y, 0.0f);
			circle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = circle;
			m_numObject++;
		}
		else if (m_state == POLYGON)
		{
			//Create a rectangle object
			CTriangle *triangle = new CTriangle();
			triangle->getBegin()->setCoords(x, y, 0.0f);
			triangle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = triangle;
			m_numObject++;
		}
		else if (m_state == SPHERE)
		{
			//Create a rectangle object
			CSphere *triangle = new CSphere();
			triangle->getBegin()->setCoords(x, y, 0.0f);
			triangle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = triangle;
			m_numObject++;
		}
		else if (m_state == CUBE)
		{
			//Create a rectangle object
			CCube *triangle = new CCube();
			triangle->getBegin()->setCoords(x, y, 0.0f);
			triangle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = triangle;
			m_numObject++;
		}
		else if (m_state == CYLINDER)
		{
			//Create a rectangle object
			CCylinder *triangle = new CCylinder();
			triangle->getBegin()->setCoords(x, y, 0.0f);
			triangle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = triangle;
			m_numObject++;
		}
		else if (m_state == TORUS)
		{
			//Create a rectangle object
			CTorus *triangle = new CTorus();
			triangle->getBegin()->setCoords(x, y, 0.0f);
			triangle->getEnd()->setCoords(x, y, 0.0f);
			m_listObjects[m_numObject] = triangle;
			m_numObject++;
		}
	}	
}

void CMFCLab2View::OnLButtonUp(UINT nFlags, CPoint point)
{
	m_isMouseDown = false;
	//Invalidate();
}

void CMFCLab2View::OnMouseMove(UINT nFlags, CPoint point)
{
	if (m_isMouseDown)
	{
		CRect rect; 
		GetClientRect(rect); 
		//Change from window coordinate system to real coordinate system
		float x = 2* point.x * m_width / rect.Width() - m_width;
		float y = 2*(rect.Height() - point.y)*m_height / rect.Height() - m_height;
		if (m_state == SELECT)
		{
			float x1 = 2* lastPoint.x * m_width / rect.Width() - m_width;
			float y1 = 2*(rect.Height() - lastPoint.y)*m_height / rect.Height() - m_height;

			float x_offset = x - x1;
			float y_offset = y - y1;

			CGeometricObject *selectedObject = NULL;
			for (int i = 0; i < m_numObject; i++)
			{
				CGeometricObject *geoObject = m_listObjects[i];
				if (geoObject->selected)
				{
					selectedObject = geoObject;
					break;
				}
			}
			if (selectedObject == NULL) return;

			CPoint3D *point = selectedObject->getBegin();
			float newValue = point->getX() + x_offset;
			//point->setX(newValue);
			point->setCoords(newValue, point->getY(), point->getZ(), point->getW());

			point = selectedObject->getEnd();
			newValue = point->getX() + x_offset;
			point->setCoords(newValue, point->getY(), point->getZ(), point->getW());

			point = selectedObject->getBegin();
			 newValue = point->getY() + y_offset;
			//point->setX(newValue);
			point->setCoords(point->getX(), newValue, point->getZ(), point->getW());

			point = selectedObject->getEnd();
			newValue = point->getY() + y_offset;
			point->setCoords(point->getX(), newValue, point->getZ(), point->getW());

		}
		if (m_state == LINE)
		{
			//Adjust the line object
			CLine* line = (CLine*) m_listObjects[m_numObject -1];	
			line->getEnd()->setCoords(x, y, 0.0f);
			//Invalidate();
		}
		else if (m_state == RECTANGLE)
		{
			CRectangle *rect = (CRectangle*) m_listObjects[m_numObject - 1];
			rect->getEnd()->setCoords(x, y, 0.0f);
			//Invalidate();
		}
		else if (m_state == CIRCLE)
		{
			CCircle *circle = (CCircle*) m_listObjects[m_numObject - 1];
			circle->getEnd()->setCoords(x, y, 0.0f);
			//Invalidate();
		}
		else if (m_state == POLYGON)
		{
			CGeometricObject *triangle = (CGeometricObject*) m_listObjects[m_numObject - 1];
			triangle->getEnd()->setCoords(x, y, 0.0f);
			//Invalidate();
		}
		else if (m_state == SPHERE)
		{

			CGeometricObject *triangle = (CGeometricObject*) m_listObjects[m_numObject - 1];
			triangle->getEnd()->setCoords(x, y, 0.0f);
		}
		else if (m_state == CUBE)
		{

			CGeometricObject *triangle = (CGeometricObject*) m_listObjects[m_numObject - 1];
			triangle->getEnd()->setCoords(x, y, 0.0f);
		}
		else if (m_state == CYLINDER)
		{

			CGeometricObject *triangle = (CGeometricObject*) m_listObjects[m_numObject - 1];
			triangle->getEnd()->setCoords(x, y, 0.0f);
		}
		else if (m_state == TORUS)
		{

			CGeometricObject *triangle = (CGeometricObject*) m_listObjects[m_numObject - 1];
			triangle->getEnd()->setCoords(x, y, 0.0f);
		}
	}

	lastPoint = point;
}

BOOL CMFCLab2View::OnEraseBkgnd(CDC* pDC)
{
	return true;
}

void CMFCLab2View::OnToolBarButtonSelect()
{
	m_state = SELECT;
	//m_numObject = 0;
	//Invalidate();
	//UINT setTimerResult =  
	
}
void CMFCLab2View::OnToolBarButtonLine()
{
	m_state = LINE;
}

void CMFCLab2View::OnToolBarButtonRectangle()
{
	m_state = RECTANGLE;
}

void CMFCLab2View::OnToolBarButtonCircle()
{
	m_state = CIRCLE;
}

void CMFCLab2View::OnToolBarButtonPolygon()
{
	m_state = POLYGON;
}

void CMFCLab2View::OnToolBarButtonCube()
{
	m_state = CUBE;
}


void CMFCLab2View::OnToolBarButtonSphere()
{
	m_state = SPHERE;
}


void CMFCLab2View::OnToolBarButtonCylinder()
{
	m_state = CYLINDER;
}

void CMFCLab2View::OnToolBarButtonTorus()
{
	m_state = TORUS;
}

void CMFCLab2View::OnUpdateToolBarButtonSelect(CCmdUI *pCmdUI)
{
	pCmdUI->SetRadio(m_state==SELECT);
}

void CMFCLab2View::OnUpdateToolBarButtonLine(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == TWO_D);
	pCmdUI->SetRadio(m_state==LINE);
}

void CMFCLab2View::OnUpdateToolBarButtonRectangle(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == TWO_D);
	pCmdUI->SetRadio(m_state==RECTANGLE);	
}

void CMFCLab2View::OnUpdateToolBarButtonCircle(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == TWO_D);
	pCmdUI->SetRadio(m_state==CIRCLE);
}

void CMFCLab2View::OnUpdateToolBarButtonPolygon(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == TWO_D);
	pCmdUI->SetRadio(m_state==POLYGON);
}

void CMFCLab2View::OnUpdateToolBarButtonCube(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == THREE_D);
	pCmdUI->SetRadio(m_state==CUBE);
}


void CMFCLab2View::OnUpdateToolBarButtonSphere(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == THREE_D);
	pCmdUI->SetRadio(m_state==SPHERE);
}


void CMFCLab2View::OnUpdateToolBarButtonCylinder(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == THREE_D);
	pCmdUI->SetRadio(m_state==CYLINDER);
}

void CMFCLab2View::OnUpdateToolBarButtonTorus(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_mode == THREE_D);
	pCmdUI->SetRadio(m_state==TORUS);
}

void CMFCLab2View::OnView2D()
{
	m_mode = TWO_D;	
	m_numObject = 0;
}

void CMFCLab2View::OnView3D()
{
	m_mode = THREE_D;
	m_numObject = 0;
}

void CMFCLab2View::OnUpdateView2D(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_mode == TWO_D);
}

void CMFCLab2View::OnUpdateView3D(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(m_mode == THREE_D);
}



void CMFCLab2View::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	Invalidate();
	CView::OnTimer(nIDEvent);
}


void CMFCLab2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	int KEY_MASK = - 128;
	short isUpcase =(GetKeyState(VK_SHIFT) & KEY_MASK);
	//isUpcase = isUpcase || (GetKeyState(VK_CAPITAL));
	CGeometricObject *selectedObject = NULL;
	for (int i = 0; i < m_numObject; i++)
	{
		CGeometricObject *geoObject = m_listObjects[i];
		if (geoObject->selected)
		{
			selectedObject = geoObject;
			break;
		}
	}
	

	float increaseValue;
	int rotateValue;
	if (isUpcase)
	{
		increaseValue = .05;
		rotateValue = 2;
	}
	else
	{
		increaseValue = -.05;
		rotateValue = -2;
	}

	switch (nChar)
	{
		case 'X':
			{
				if (selectedObject == NULL) return;
			CPoint3D *point = selectedObject->getBegin();
			float newValue = point->getX() + increaseValue;
			//point->setX(newValue);
			point->setCoords(newValue, point->getY(), point->getZ(), point->getW());

			point = selectedObject->getEnd();
			newValue = point->getX() + increaseValue;
			point->setCoords(newValue, point->getY(), point->getZ(), point->getW());
			break;
			}
		case 'Y':
			{

				if (selectedObject == NULL) return;
				CPoint3D *point = selectedObject->getBegin();
				float newValue = point->getY() + increaseValue;
				//point->setX(newValue);
				point->setCoords(point->getX(), newValue, point->getZ(), point->getW());

				point = selectedObject->getEnd();
				newValue = point->getY() + increaseValue;
				point->setCoords(point->getX(), newValue, point->getZ(), point->getW());
				break;
			}
		case 'Z':
			{

				if (selectedObject == NULL) return;
				CPoint3D *point = selectedObject->getBegin();
				float newValue = point->getZ() + increaseValue;
				//point->setX(newValue);
				point->setCoords(point->getX(), point->getY(), newValue, point->getW());

				point = selectedObject->getEnd();
				newValue = point->getZ() + increaseValue;
				point->setCoords(point->getX(), point->getY(), newValue, point->getW());
				break;
			}
		case 'O': //zoom
			{
				if (m_mode == THREE_D)
				{
					camera3d_location_x += increaseValue;
					camera3d_location_y += increaseValue;
					camera3d_location_z += increaseValue;
				}
				if (m_mode == TWO_D)
				{
					camera2d_location_z += increaseValue;
				}
			}
		case 'W':
			{

				if (selectedObject == NULL) return;
				selectedObject->rotateX += rotateValue;
				//selectedObject->rotateX = selectedObject->rotateX % 360;
				break;
			}
		case 'E':
			{

				if (selectedObject == NULL) return;
				selectedObject->rotateY += rotateValue;
				break;
			}
		case 'R':
			{

				if (selectedObject == NULL) return;
				selectedObject->rotateZ += rotateValue;
				break;
			}
		case 'S':
			{

				if (selectedObject == NULL) return;
				CPoint3D *point = selectedObject->getBegin();
				float newValueX = point->getX() - increaseValue;
				float newValueY = point->getY() + increaseValue;
				point->setCoords(newValueX, newValueY, point->getZ(), point->getW());

				point = selectedObject->getEnd();
				newValueX = point->getX() + increaseValue;
				newValueY = point->getY() - increaseValue;
				point->setCoords(newValueX, newValueY, point->getZ(), point->getW());
				break;
			}
	}
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
